//
//  RegisterOneViewController.m
//  HTP
//
//  Created by 陈钱钿 on 15/11/1.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "RegisterOneViewController.h"
#import "RegisterTwoViewController.h"


@interface RegisterOneViewController ()
@property (nonatomic, retain) UITextField *Field;
@property (nonatomic, retain) UIButton *getBtn;
@property (nonatomic, retain) UIButton *selectBtn;
@end

@implementation RegisterOneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //监听文本改动
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.Field];
    [self initUI];
    
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
    [self addCustomTitleWithTitle:@"验证手机号码"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
}
//TODO:绘制UI
- (void)initUI {
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(0, 20, 15, 20);
    [back setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(doBack) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backItem;
    
    self.Field = [[UITextField alloc] initWithFrame:CGRectMake((ScreenWidth-150)/2, ScreenHeihgt/4, 150, 30)];
    self.Field.placeholder = @"   请输入手机号";
    self.Field.keyboardType = UIKeyboardTypePhonePad;
    self.Field.font = [UIFont systemFontOfSize:18];
    [self.Field setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.view addSubview:self.Field];
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-200)/2, CGRectGetMaxY(self.Field.frame), 200, 2)];
    line.image = [UIImage imageNamed:@"black_line"];
    line.alpha = 0.5;
    [self.view addSubview:line];
    
    self.getBtn = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-200)/2, CGRectGetMaxY(line.frame)+50, 200, 40)];
    [self.getBtn setBackgroundImage:[UIImage imageNamed:@"get_gray"] forState:UIControlStateNormal];
    self.getBtn.enabled = NO;
    [self.getBtn addTarget:self action:@selector(getMessageClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.getBtn];
    
    self.selectBtn = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-200)/2, CGRectGetMaxY(self.getBtn.frame)+10, 15, 15)];
    //未选择是1 选中是2
    self.selectBtn.tag = 1;
    [self.selectBtn setBackgroundImage:[UIImage imageNamed:@"primary"] forState:UIControlStateNormal];
    [self.selectBtn addTarget:self action:@selector(selectBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.selectBtn];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.selectBtn.frame)+5, CGRectGetMaxY(self.getBtn.frame)+10, 230, 15)];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"我已阅读并同意青苗家教《用户协议》"];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 7)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(7, 10)];
    label.attributedText = str;
    label.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:label];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

/*
 监听 文本框内改变
 */
- (void)textChange {
    if(self.Field.text.length == 11 && self.selectBtn.tag == 2){
        [self.getBtn setBackgroundImage:[UIImage imageNamed:@"get_light"] forState:UIControlStateNormal];
        self.getBtn.enabled = YES;
    } else{
        [self.getBtn setBackgroundImage:[UIImage imageNamed:@"get_gray"] forState:UIControlStateNormal];
        self.getBtn.enabled = NO;
    }
}

- (void)doBack {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
 阅读协议框的点击
 */
- (void)selectBtnClick {
    if(self.selectBtn.tag == 1){ //未选中改为选中
        [self.selectBtn setBackgroundImage:[UIImage imageNamed:@"tick"] forState:UIControlStateNormal];
        if(self.Field.text.length == 11){
            [self.getBtn setBackgroundImage:[UIImage imageNamed:@"get_light"] forState:UIControlStateNormal];
            self.getBtn.enabled = YES;
        }else {
            [self.getBtn setBackgroundImage:[UIImage imageNamed:@"get_gray"] forState:UIControlStateNormal];
            self.getBtn.enabled = NO;
        }
        self.selectBtn.tag = 2;
    } else { //选中改为未选中
        [self.selectBtn setBackgroundImage:[UIImage imageNamed:@"primary"] forState:UIControlStateNormal];
        self.selectBtn.tag = 1;
        [self.getBtn setBackgroundImage:[UIImage imageNamed:@"get_gray"] forState:UIControlStateNormal];
        self.getBtn.enabled = NO;
    }
}

/*
 获取短信验证码的点击
 */
- (void)getMessageClick {
    RegisterTwoViewController *twoVc = [[RegisterTwoViewController alloc] init];
    [self.navigationController pushViewController:twoVc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
